//
//  YoutubeViewController.m
//  IntroductionApp
//
//  Created by 英司 菅澤 on 12/02/13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "YoutubeViewController.h"

#import "youtubeDetailViewController.h"
#import "youtubeTableCell.h"


@implementation YoutubeViewController

@synthesize movieTable = _movieTable;
@synthesize movieAry = _movieAry;
@synthesize ApiRequest = _ApiRequest;
@synthesize isGetYoutube = _isGetYoutube;
@synthesize FeedParser = _FeedParser;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // youtube成功Flg
    self.isGetYoutube = NO;
    
    // 配列初期化
    self.movieAry = [[ NSMutableArray alloc ]init];
    
    // tableView 
    self.movieTable.dataSource = self;
    self.movieTable.delegate   = self;
    
    // Youtube通信
    self.ApiRequest = [[ SAConnectedApi alloc ]init];
    self.ApiRequest.Delegate = self;
    [ self.ApiRequest getYouTubeData ];
    
    // 背景色
    [ self.movieTable setBackgroundColor:[ UIColor clearColor] ];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    _movieTable = nil;
}

-(void)dealloc{
    
    [ _movieTable release ];
    [ _ApiRequest release ];
    [ _FeedParser release ];
    [ _movieAry release ];
    
    [ super dealloc ];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


/* ====================================================================================== */
#pragma mark - TableView Data souce
/* ====================================================================================== */

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [ self.movieAry count ];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"youtubeTableCell";
    
    youtubeTableCell *cell = (youtubeTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[ NSBundle mainBundle ] loadNibNamed:@"youtubeTableCell" 
                                                owner:self
                                              options:nil]lastObject];
    }
    // BG
    NSArray *cellAry = [[NSArray alloc] initWithObjects:cell,indexPath, nil];
    [ self performSelectorInBackground:@selector(CreateCellonBG:) withObject:cellAry ];
    [ cellAry release ];
    
    return cell;
}

// backGround
-(void)CreateCellonBG:(NSArray*)cellAry{
    
    NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc ]init];
    
    youtubeTableCell *cell = [ cellAry objectAtIndex:0 ];
    NSIndexPath *path = [ cellAry objectAtIndex:1 ];
    NSDictionary *dic = [ self.movieAry objectAtIndex:path.row ];
    
    // cellのセット
    [ cell performSelectorOnMainThread:@selector(setCellData:) withObject:dic waitUntilDone:YES ];
    
    // サムネ
    NSURL* url = [NSURL URLWithString:[ dic objectForKey:@"thumb" ]];
    NSData* data = [NSData dataWithContentsOfURL:url];
    UIImage* img = [[UIImage alloc] initWithData:data];
    
    [ cell.picView performSelectorOnMainThread:@selector(setImage:) withObject:img waitUntilDone:YES ];
    
    [ pool release ];
}

// delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 行選択後
    youtubeDetailViewController *playerCon = [[ youtubeDetailViewController alloc ]initWithNibName:@"youtubeDetailViewController" bundle:nil];
    
    [ self.navigationController pushViewController:playerCon animated:YES ];
    
    [ playerCon setMovieData: [ self.movieAry objectAtIndex:indexPath.row ] ];
    
    [ playerCon release ];
    
}

/* ====================================================================================== */
#pragma mark - ASIHTTP method
/* ====================================================================================== */

- (void)requestFinished:(SAConnectedApi*)object:(NSString*)request{
    
    if([ request length ] > 0){
        self.movieAry = [request objectFromJSONString];
        
        [ self.movieTable reloadData ];
        // 送信Flg
        self.isGetYoutube = YES;
    
        
    }
}

- (void)requestFailed:(SAConnectedApi*)object:(NSError*)error{
    NSLog(@"error = %@",[ error description ]);
    
}

@end
